package com.oocl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.entity.Foods;
import com.oocl.service.FoodAdService;
import com.oocl.util.RequestUtil;
import com.oocl.vo.FoodAdVo;
import com.oocl.vo.Foodmodel;

@Controller
@RequestMapping("/foodAd")
public class FooAdController {
	@Resource(name="foodAdService")
	private FoodAdService foodAdService;
	@Value("#{configProperties['pushAd']}")
	   private String pushAd;
	
	
	@RequestMapping(value="/addFoodAd")
	@ResponseBody
	public FoodAdVo addFoodAd( @RequestBody Foods food,HttpSession session){
		System.out.println(food.getFid());
		FoodAdVo fa = foodAdService.addFoodAd(food.getFid(), (String)session.getAttribute("icnum"));
		String uri = pushAd;
		String params = "&merchant="+fa.getMerchant().getIcnum()+"&food="+fa.getFood().getFname()+"&picUrl="+fa.getFood().getFimg()+"&price="+fa.getFood().getFprice();
		Map<String,String> info = (Map) RequestUtil.request(uri, "POST", params, Map.class);
		if(info!=null){
			String status = info.get("status");
			if("200".equals(status)){
				return fa;			
			}
		}
		return new FoodAdVo();
	}
	
	@RequestMapping(value="/findAllFoodAd",method=RequestMethod.GET)
	@ResponseBody
	public List<FoodAdVo> findAllFoodAds(HttpSession session){	
		return foodAdService.findFoodAdByMerchant((String)session.getAttribute("icnum"));		
	}
	
	@RequestMapping(value="findPassed",method=RequestMethod.GET)
	@ResponseBody
	public List<FoodAdVo> findPassedFoodAds(HttpSession session){
		return foodAdService.findPassedFoodAdByMerchant((String)session.getAttribute("icnum"));
		
	}
	
	@RequestMapping(value="findUnpassed",method=RequestMethod.GET)
	@ResponseBody
	public List<FoodAdVo> findUnpassedFoodAds(HttpSession session){
		return foodAdService.findUnpassedFoodAdByMerchant((String)session.getAttribute("icnum"));
		
	}
	
	@RequestMapping(value="updateAdfromA")
	@ResponseBody
	public String updateAdfromA(Foodmodel fd){
		System.out.println(fd.getFood());
		System.out.println(fd.getMerchant());
		System.out.println(fd.getStatus());
		foodAdService.updateAdStatus(fd.getFood(), fd.getMerchant(), fd.getStatus());
		Map<String,String> map = new HashMap<String,String>();
		map.put("status", "200");
		return RequestUtil.wrapToJson(map);	
	}
	
	
	

}
